package com.qf.dao.impl;

import com.qf.dao.OrderderDao;
import com.qf.domain.Order;
import com.qf.domain.OrderDetail;
import com.qf.util.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderderDaoImpl implements OrderderDao {
    private QueryRunner queryRunner = new QueryRunner();

    @Override//添加订单
    public void add(Order order) {
        //获取连接
        Connection conn = DataSourceUtil.getConnection();
        String sql = "insert into tb_order (id,uid,money,status,time,aid) values (?,?,?,?,?,?)";
        Object[] params = {order.getId(), order.getUid(), order.getMoney(), order.getStatus(), order.getTime(), order.getAid()};
        try {
            queryRunner.update(conn, sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtil.closeAll(null, null, conn);
        }
    }

    @Override//订单详情
    public void addDetail(OrderDetail orderDetail) {
        //获取连接
        Connection conn = DataSourceUtil.getConnection();
        String sql = "insert into tb_orderdetail (oid,pid,num,money) values(?,?,?,?)";
        Object[] params = {orderDetail.getOid(),orderDetail.getPid(),orderDetail.getNum(),orderDetail.getMoney()};
        try {
            queryRunner.update(conn, sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            //关闭事务
            DataSourceUtil.closeAll(null, null, conn);
        }
    }

    @Override//修改订单状态
    public void updateState(String oid, int state) {
        //获取连接
        Connection conn = DataSourceUtil.getConnection();
        String sql="update tb_order set status=2 where id=?";
        try {
            queryRunner.update(conn,sql,oid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,conn);
        }
    }

    @Override
    public List<Order> select(Integer uid) {
        Connection conn = DataSourceUtil.getConnection();
        try {
            return queryRunner.query(conn,"select * from tb_order where uid=?",new BeanListHandler<>(Order.class),uid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,conn);
        }
    }

    @Override
    public Order selectByOid(String oid) {
        Connection conn = DataSourceUtil.getConnection();
        try {
            return queryRunner.query(conn,"select * from tb_order where id=?",new BeanHandler<>(Order.class),oid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,conn);
        }
    }

    @Override
    public List<OrderDetail> selectDetail(String oid) {
        Connection conn = DataSourceUtil.getConnection();
        try {
            return queryRunner.query(conn,"select * from tb_orderdetail where oid=?",new BeanListHandler<>(OrderDetail.class),oid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.closeAll(null,null,conn);
        }
    }
}
